import Vue                  from 'vue'
import Vuex                 from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import getters              from './getters'
import defaultDictionary    from '@/common/dictionary'
import api                  from "@/api/api"
import request              from "@/api/axiosInstance"

Vue.use(Vuex)

export default new Vuex.Store({
	plugins  : [
		createPersistedState({
			storage: window.sessionStorage
		})
	],
	getters,
	state    : {
		firstLogin       : true,//用户第1次登录
		defaultActive    : '', // 注意传参必须带斜杠, 如: '/ManagTable'
		belongTo         : '', //
		token            : undefined, //'3523ff19-fe28-4f58-8663-a9480e07912c'
		sessionUser      : undefined, //_session_user
		sessionOrg       : undefined, //_session_org
		userInfo         : null, //系统头会用到
		menuData         : null, //
		dictionaryMapping: defaultDictionary,
		selectedTask     : null, //{ id, isTask, spFlag, taskPzId, taskSzId ... }
		disableTaskBtn   : false,//disabled
		headerTotal      : {
			approvalTotal   : 0,
			rejectTotal     : 0,
			supervisionTotal: 0,
			warningTotal    : 0,
			rejectList      : [],
		}
	},
	mutations: {
		setState(state, payload) {
			// if (payload.hasOwnProperty('defaultActive')) state.defaultActive = payload.defaultActive;
			// if (payload.hasOwnProperty('token')) state.token = payload.token;
			// if (payload.hasOwnProperty('sessionUser')) state.sessionUser = payload.sessionUser;
			// if (payload.hasOwnProperty('sessionOrg')) state.sessionOrg = payload.sessionOrg;
			// if (payload.hasOwnProperty('userInfo')) state.userInfo = payload.userInfo;
			
			// console.log('Object.keys(payload)', Object.keys(payload));
			Object.keys(payload).forEach(key => state[key] = payload[key])
		},
		setSelectedTask(state, selectedTask) {
			console.log('store setSelectedTask', selectedTask)
			state.selectedTask = selectedTask
		},
		setApproval(state, { total }) {
			state.headerTotal.approvalTotal = total
		},
		setReject(state, { total, list = [] }) {
			state.headerTotal.rejectTotal = total || list.length
			state.headerTotal.rejectList = list
		},
		setSupervision(state, { total }) {
			state.headerTotal.supervisionTotal = total
		},
		setWarning(state, { total }) {
			state.headerTotal.warningTotal = total
		},
		logout(state) {
			state.firstLogin = true
			state.defaultActive = ''
			state.token = ''
			state.sessionUser = ''
			state.sessionOrg = ''
			state.userInfo = ''
			sessionStorage.clear()
		}
	},
	actions  : {
		// 动态切换主题色
		async changeSelectedTask({ commit, state, dispatch }, payload) {
			await commit('setSelectedTask', payload)
		},
		async getHeaderTotals({ commit, state, dispatch }) {
			await Promise.all([
				dispatch('getApprovalList'),
				dispatch('getRejectList'),
				dispatch('getSupervisionList'),
				dispatch('getWarningList'),
			])
		},
		async getApprovalList({ commit }) {
			const res = await api.get["/rest/rptSp/querySpList"](null, null, null, null, null, null, 0, 0, 100000)
			const { data: { data: resData = {} } = {} } = res
			const { payload } = resData
			if (payload) {
				commit('setApproval', { total: payload.total })
			}
		},
		
		async getRejectList({ commit }) {
			const res = await api.get["/rest/rptSp/queryRefuse"]()
			const { data: { data: resData = {} } = {} } = res
			commit('setReject', { total: resData.total || resData.length, list: resData })
		},
		
		async getSupervisionList({ commit }) {
			
			const res = await api.get["/rest/taskSupervise/queryTaskSuperviseWarnList"](null, null, null, null, null, '01', 0, 10000)
			const { data: { data: resData = {} } = {} } = res
			const { payload } = resData
			if (payload) {
				commit('setSupervision', { total: payload.total })
			}
		},
		
		async getWarningList({ commit }) {
			const res = await api.get["/rest/taskSupervise/queryTaskOvertimeList"](null, null, null, 1, 0, 10000, '01')
			const { data: { data: resData = {} } = {} } = res
			const { payload } = resData
			if (payload) {
				commit('setWarning', { total: payload.total })
			}
		},
	}
})
